import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */
/* Router Modules */
import componentsRouter from './modules/components'
/**
 * constantRoutes
 * 基础页面，无权限要求
 * 所有角色都可访问
 */
export const constantRoutes = [{
        path: '/login',
        component: () =>
            import ('@/views/login/index'),
        hidden: true
    },
    //  componentsRouter, //   组件预览库
    {
        path: '/404',
        component: () =>
            import ('@/views/error-page/404'),
        hidden: true
    },
    {
        path: '/401',
        component: () =>
            import ('@/views/error-page/401'),
        hidden: true
    },
   

    {
        path: '/',
        component: Layout,
        redirect: '/dashboard',
        children: [{
            path: 'dashboard',
            name: 'Dashboard',
            component: () =>
                import ('@/views/dashboard/index'),
            meta: { title: '主页', icon: 'dashboard' }
        }]
    },
    {
        path: '/',
        component: Layout,
        redirect: '/notice',
        children: [{
            path: 'notice',
            name: 'notice',
            component: () =>
                import ('@/views/notice/notice'),
            meta: { title: '系统公告', icon: 'notice' }
        }]
    },
    

]

const createRouter = () => new Router({
    mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    base: '/',
    routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher // reset router
}

//异步获取需要的路由
export const asyncRouterMap = [

    {
        path: '/facility',
        component: Layout,
        redirect: '/facility/index',
        meta: { role: ['admin','editor','serviceman'], title: '设备管理', icon: 'form' },
        children: [{
                path: 'facFaultMessage',
                name: 'facFaultMessage',
                component: () =>
                    import ('@/views/facility/facFaultMessage'),
                meta: { role: ['admin','editor','serviceman'], title: '故障信息管理', icon: 'form' },
            }, {
                path: 'faclist',
                name: 'faclist',
                component: () =>
                    import ('@/views/facility/index'),
                meta: { role: ['admin','editor','serviceman'], title: '设备列表', icon: 'form' },

            }, {
                path: 'addFac',
                name: 'addFac',
                component: () =>
                    import ('@/views/facility/addFac'),
                meta: { title: '新增设备', icon: 'form' },
                hidden: true

            }, {
                path: 'facMessage',
                name: 'facMessage',
                component: () =>
                    import ('@/views/facility/facMessage'),
                meta: { role: ['admin','editor','serviceman'], title: '设备详情', icon: 'form' },
                hidden: true
            },
            // {
            //     path: 'facRepair',
            //     name: 'facRepair',
            //     component: () =>
            //         import ('@/views/facility/facRepair'),
            //     meta: { role: ['admin','editor','serviceman'], title: '报修管理', icon: 'form' },
            // },
            {
                path: 'facRe',
                name: 'facRe',
                component: () =>
                    import ('@/views/facility/facRe'),
                meta: { role: ['admin','editor','serviceman'], title: '设备报修', icon: 'form' },
                hidden: true
            },

            {
                path: 'facMaintain',
                name: 'facMaintain',
                component: () =>
                    import ('@/views/facility/facMaintain'),
                meta: { role: ['admin','editor','serviceman'], title: '设备保养', icon: 'form' },
                hidden: true
            },
            {
                path: 'facMain',
                name: 'facMain',
                component: () =>
                    import ('@/views/facility/facMain'),
                meta: { role: ['admin','editor','serviceman'], title: '保养管理', icon: 'form' },
            },
            {
                path: 'facStatistics',
                name: 'facStatistics',
                component: () =>
                    import ('@/views/facility/facStatistics'),
                meta: { role: ['admin','editor',], title: '设备统计', icon: 'form' },
            }
        ],


    },
    {
        path: '/error',
        component: Layout,
        redirect: 'noRedirect',
        name: 'ErrorPages',
        meta: {
            title: 'errorPages',
            icon: '404'
        },
        hidden: true,
        children: [{
                path: '401',
                component: () =>
                    import ('@/views/error-page/401'),
                name: 'Page401',
                meta: { title: 'page401', noCache: true }
            },
            {
                path: '404',
                component: () =>
                    import ('@/views/error-page/404'),
                name: 'Page404',
                meta: { title: 'page404', noCache: true }
            }
        ]
    },
    {
        path: '/user',
        component: Layout,
        redirect: '/user/index',
        name: 'user',
        meta: { role: ['admin', 'editor'], title: '用户管理', icon: 'form' }, //页面需要的权限
        children: [{
                path: 'index',
                name: 'userList',
                component: () =>
                    import ('@/views/user/index'),
                meta: { role: ['admin', 'editor'], title: '用户列表', icon: 'form' }
            }, {
                path: 'permission',
                component: () =>
                    import ('@/views/user/permission'),
                name: 'userPermission',
                meta: { role: ['admin', ] }, //页面需要的权限
                children: [{
                    path: 'index',
                    name: 'permission',
                    component: () =>
                        import ('@/views/user/permission'),
                    meta: { role: ['admin'], title: '权限测试', icon: 'form' }
                }],
            },
            {
                path: 'usermessage',
                component: () =>
                    import ('@/views/user/usermessage'),
                name: 'userMessage',
                meta: {}, //页面需要的权限
                children: [{
                    path: 'index',
                    name: 'message',
                    component: () =>
                        import ('@/views/user/usermessage'),
                    meta: { title: '用户中心', icon: 'form' }
                }],
            },
        ],
    },
    {
        path: '/system',
        component: Layout,
        redirect: '/system/index',
        name: 'System',
        meta: { role: ['admin'], title: '系统管理', icon: 'form' }, //页面需要的权限
        children: [{
            path: 'index',
            name: 'basic',
            component: () =>
                import ('@/views/system/basic'),
            meta: { role: ['admin'], title: '数据字典', icon: 'form' }
        },{
            path: 'barcode',
            name: 'barcode',
            component: () =>
                import ('@/views/system/barcode'),
            meta: {  title: '条码生成', icon: 'form' }
        }, ],
    },
    
    // 404 page must be placed at the end !!!
    //添加404就内存溢出 原因待查
    { path: '*', redirect: '/404', hidden: true }
    //   {
    //     path: '/404',
    //     component: () =>
    //         import ('@/views/404'),
    //     hidden: true
    // },
]
export default router